package me.zhengjie.modules.schedule;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.system.domain.PokerActivateInfo;
import me.zhengjie.modules.system.service.AutoPokerActivateConfigService;
import me.zhengjie.modules.system.service.PokerActivateInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Configuration
@EnableScheduling
@RequiredArgsConstructor
public class ActivateScheduleTask {

    @Autowired
    PokerActivateInfoService activateInfoService;

    @Autowired
    PokerActivateInfoService pokerActivateInfoService;

    @Autowired
    AutoPokerActivateConfigService configService;



    @Scheduled(cron = "0 0/1 * * * ? ")
    public void schedule() {
        log.info("poker activate task end .......");
        LambdaQueryWrapper<PokerActivateInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PokerActivateInfo::getRunStatus,1).eq(PokerActivateInfo::getActivateType,0);
        List<PokerActivateInfo> list = activateInfoService.list(wrapper);
        LocalDateTime now = LocalDateTime.now();
        for (PokerActivateInfo pokerActivateInfo : list) {
            // 获取活动的结束时间
            Timestamp endTimeTimestamp = pokerActivateInfo.getEndTime();
            if (endTimeTimestamp != null) {
                LocalDateTime endTime = endTimeTimestamp.toLocalDateTime();
                if (endTime.isBefore(now)) {
                    // 执行你需要的处理逻辑
                    pokerActivateInfoService.endActivate(pokerActivateInfo.getId());
                }
            }
        }
    }

//    定时生成卡牌活动
    @Scheduled(cron = "0 0 1 * * ?")
//    @Scheduled(cron = "0 0/1 * * * ? ")
    public void taskGen() {
        log.info("poker activate gen task start .......");
        configService.autoGenPokerActivate();
    }



}
